package com.darker.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.darker.reggie.common.R;
import com.darker.reggie.dto.SetmealDto;
import com.darker.reggie.entity.Category;
import com.darker.reggie.entity.Setmeal;
import com.darker.reggie.service.CategoryService;
import com.darker.reggie.service.SetmealDishService;
import com.darker.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Resource
    SetmealService setmealService;
    @Resource
    CategoryService categoryService;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //查询套餐信息
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, queryWrapper);


        //从setmealPage读取数据到setmealDtoPage 除了records属性
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        //处理setmealDtoPage的records属性
        //查询套餐分类信息
        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);

            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {

                setmealDto.setCategoryName(category.getName());
                setmealDtoList.add(setmealDto);
            }

        }
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    //删除套餐 以及套餐对应的菜品关系
    @DeleteMapping
    public R<String> delByid(@RequestParam List<Long> ids) {
        setmealService.delById(ids);
        return R.success("删除成功");
    }
    //根据id查询在售套餐
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        List<Setmeal> list=null;
        String key = "setmeal_" + setmeal.getCategoryId() + "_" + setmeal.getStatus();
        list = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if(list!=null) return R.success(list);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(Setmeal::getStatus, setmeal.getStatus());
        list  = setmealService.list(queryWrapper);

        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }
    //根据ID查询套餐信息
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealDtoById(@PathVariable long id){
        SetmealDto setmealDto = setmealService.getSetmealDto(id);
        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> updateSteamlDto(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
    //批量启售停售
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> queryWrapper=new LambdaUpdateWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.set(Setmeal::getStatus,status);
        setmealService.update(queryWrapper);
        return R.success("状态更新成功");

    }


}
